package com.ruoyi.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.GlobalInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pms.domain.PmsTask;
import com.ruoyi.system.controller.SysSerialRuleController;
import com.ruoyi.system.service.impl.SysSerialRuleServiceImpl;
import io.jsonwebtoken.lang.Collections;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pms.mapper.PmsProjectMapper;
import com.ruoyi.pms.domain.PmsProject;
import com.ruoyi.pms.service.IPmsProjectService;

/**
 * 项目信息Service业务层处理
 *
 * @author huikai
 * @date 2025-03-18
 */
@Service
public class PmsProjectServiceImpl extends ServiceImpl<PmsProjectMapper, PmsProject> implements IPmsProjectService {
    @Autowired
    private PmsProjectMapper pmsProjectMapper;
    @Autowired
    private PmsTaskServiceImpl pmsTaskServiceImpl;

    @Autowired
    private SysSerialRuleServiceImpl sysSerialRuleService;

    /**
     * 查询项目信息列表
     *
     * @param pmsProject 项目信息
     * @return 项目信息
     */
    @Override
    public List<PmsProject> selectPmsProjectList(PmsProject pmsProject) {
        return pmsProjectMapper.selectPmsProjectList(pmsProject);
    }


    /**
    * 查询项目信息记录
    * @param projectId
    * @return
    */
    @Override
    public PmsProject getInfoByProjectId(String projectId) {
        return pmsProjectMapper.selectById(projectId);
    }

    /**
     * 新增项目信息记录
     * @param pmsProject 项目信息
     * @return
     */
    @Override
    public Boolean add(PmsProject pmsProject) {
        if (StringUtils.isBlank(pmsProject.getProjectId())) {
            pmsProject.setProjectId(sysSerialRuleService.generateSerialNumber("pms_project_id"));
        }
        pmsProject.setCreateBy(GlobalInfo.getUserId());
        pmsProject.setCreateTime(new Date());
        return pmsProjectMapper.insert(pmsProject) > 0;
    }

    /**
     * 修改项目信息记录
     * @param pmsProject 项目信息
     * @return
     */
    @Override
    public Boolean edit(PmsProject pmsProject) {
        pmsProject.setUpdateBy(GlobalInfo.getUserId());
        pmsProject.setUpdateTime(new Date());
        return pmsProjectMapper.updateById(pmsProject) > 0;
    }

    /**
     * 删除项目信息记录
     * @param projectIds
     * @return
     */
    @Override
    public Boolean removeByProjectIds(String[] projectIds) {
        QueryWrapper<PmsTask> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(PmsTask::getProjectId,projectIds);
        List<PmsTask> pmsTaskList = pmsTaskServiceImpl.list(queryWrapper);
        if(!Collections.isEmpty(pmsTaskList)){
            throw new RuntimeException(StringUtils.format("删除失败，项目{}下存在任务",projectIds));
        }
        return pmsProjectMapper.deleteBatchIds(Arrays.asList(projectIds)) > 0;
    }
}